//获取应用实例 
const app = getApp();

Page({
	data: {
    orderdetail:[],
    flag: 0,
    content:'',
    /**
     * 图片
     */ 
    imgs: [],
    list: '',
    upload_picture_list: [],
    order_detail_id:'',
  },
	onLoad: function(options) {
    console.log(options.order_detail_id);
    this.setData({
      orderdetail:app.globalData.orderdetail,
      order_detail_id: options.order_detail_id
    })
  },


  changeColor1: function () {
    var that = this;
    that.setData({
      flag: 1
    });
  },
  changeColor2: function () {
    var that = this;
    that.setData({
      flag: 2
    });
  },
  changeColor3: function () {
    var that = this;
    that.setData({
      flag: 3
    });
  },
  changeColor4: function () {
    var that = this;
    that.setData({
      flag: 4
    });
  },
  changeColor5: function () {
    var that = this;
    that.setData({
      flag: 5
    });
  },
  contentInput:function(e){
    var that=this;
    that.setData({
      content:e.detail.value
    })
  },

  //选择图片方法
  uploadpic: function (e) {
    let that = this //获取上下文
    let upload_picture_list = that.data.upload_picture_list
    //选择图片
    wx.chooseImage({
      count: 8, // 默认9，这里显示一次选择相册的图片数量 
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) { // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
      console.log(res)
        let tempFiles = res.tempFilePaths
        //把选择的图片 添加到集合里
        for (let i in tempFiles) {
          upload_picture_list.push(tempFiles[i])
        }
        //显示
        that.setData({
          upload_picture_list: upload_picture_list,
        });

        console.log(that.data.upload_picture_list);
      }
    })
  },
  //点击上传图片
  uploadimage() {
    wx.showLoading({
      title: '数据提交中',
    })
    let page = this
    let upload_picture_list = page.data.upload_picture_list
      wx.uploadFile({
        url: app.globalData.api + '/index.php/wap/Order/comment', //此处换上你的接口地址 
        filePath: upload_picture_list[0],
        name: 'img',
        header: {
          "Content-Type": "multipart/form-data",
        },
        formData: {
          token: app.globalData.token,
          order_detail_id: page.data.order_detail_id,
          score: page.data.flag,
          grade: 1,
          content: page.data.content,
          img:'filePath',
        }, // HTTP 请求中其他额外的 form data
        success: function (res) {
          wx.hideLoading();
          console.log(res);
          let data = JSON.parse(res.data); // 这个很关键
          if(data.code==1){
            wx.showToast({
              title: '评价成功',//提示文字
              duration: 2000,//显示时长
              mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
              icon: 'success', //图标，支持"success"、"loading"  
              success: function () { },//接口调用成功
              fail: function () { },  //接口调用失败的回调函数  
              complete: function () { } //接口调用结束的回调函数  
            })

            setTimeout(function () {
              //要延时执行的代码
              wx.navigateBack({
                delta:1
              })
            }, 2000) //延迟时间 这里是1秒
          }else{
            wx.showToast({
              title: '' + data.msg,//提示文字
              duration: 2000,//显示时长
              mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
              icon: 'none', //图标，支持"success"、"loading"  
              success: function () { },//接口调用成功
              fail: function () { },  //接口调用失败的回调函数  
              complete: function () { } //接口调用结束的回调函数  
            })
          }
        },fail:function(res){
          console.log(res)
          wx.hideLoading();
        }
      })
    let imgs = wx.setStorageSync('imgs', upload_picture_list);
  },
  // 点击删除图片
  deleteImg(e) {
    let upload_picture_list = this.data.upload_picture_list;
    let index = e.currentTarget.dataset.index;
    upload_picture_list.splice(index, 1);
    this.setData({
      upload_picture_list: upload_picture_list
    });
    console.log(this.data.upload_picture_list)
  },
  // 预览图片
  previewImg(e) {
    //获取当前图片的下标
    let index = e.currentTarget.dataset.index;
    //所有图片
    let imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});
